<template>
  <div>
    <el-dialog :title="dialogModel.title" :visible.sync="dialogModel.show" width="60%" @open="openFun">
  <div class="mod-config">
    <basic-container>
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-button type="success" icon="el-icon-search" @click="searchFilter">搜索</el-button>
          <el-button icon="el-icon-plus" type="primary" @click="showAddDialog()">新增</el-button>
        </el-form-item>
      </el-form>

      <div class="avue-crud">
        <el-table
                :data="dataList"
                border
                v-loading="dataListLoading">
            <el-table-column type="index" align="center" label="#" width="55"> </el-table-column>
                      <el-table-column
                    prop="modelName"
                    header-align="center"
                    align="center"
                    label="型号名称">
            </el-table-column>
                      <el-table-column
                    prop="modelCode"
                    header-align="center"
                    align="center"
                    label="型号编码">
            </el-table-column>
                      <el-table-column
                    prop="status"
                    header-align="center"
                    align="center"
                    label="是否启用">
            <template slot-scope="scope">
              <p v-if="scope.row.status == 1">已启用</p>
              <p v-if="scope.row.status == 2">未启用</p>
            </template>
            </el-table-column>
                      <el-table-column
                    prop="createTime"
                    header-align="center"
                    align="center"
                    label="创建时间">
            </el-table-column>
                      <el-table-column
                    prop="createUser"
                    header-align="center"
                    align="center"
                    label="创建人">
            </el-table-column>
                      <el-table-column
                    prop="updateTime"
                    header-align="center"
                    align="center"
                    label="修改时间">
            </el-table-column>
                      <el-table-column
                    prop="updateUser"
                    header-align="center"
                    align="center"
                    label="修改人">
            </el-table-column>
                    <el-table-column
                  header-align="center"
                  align="center"
                  label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" icon="el-icon-edit" @click="showEditDialog(scope.row)">修改</el-button>
              <el-button type="text" size="small" icon="el-icon-delete" @click="deleteHandle(scope.row.modelId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :current-page="pageIndex"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                :total="totalPage"
                background
                layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <DialogAddOrUpdateModel :dialogAddOrUpdateModel="dialogAddOrUpdateModel" :modelData="modelData" @handleSubmit="handleSubmit" @handleSubmitEdit="handleSubmitEdit"></DialogAddOrUpdateModel>
    </basic-container>
  </div>
    </el-dialog>
  </div>
</template>

<script>
import {fetchList, delObj} from '@/api/pbms/arcmodel'
import {getObj, addObj, putObj} from '@/api/pbms/arcmodel'
import DialogAddOrUpdateModel from "@/components/pbms/dialogAddOrUpdateModel";
import {mapGetters} from 'vuex'
export default {
  props: {
    dialogModel: Object,
    formData: Object
  },
  data() {
    return {
        visible: false,
        dataForm: {
          manufacturerCode: undefined
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        dialogAddOrUpdateModel: {
            show: false,
        },
        modelData: {},
    }
  },
    components: {
      DialogAddOrUpdateModel
    },
    computed: {
        ...mapGetters(['permissions'])
    },
  methods: {
    // 弹窗打开触发的方法
    openFun() {
        this.getDataList()
    },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        fetchList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize,
          manufacturerCode: this.formData.manufacturerCode,
        })).then(response => {
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
        })
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增按钮触发弹框
      showAddDialog () {
        this.dialogAddOrUpdateModel = {
            show: true,
            title: "新增设备型号",
            option: "add",
        };
        this.modelData = {
          modelName: '',
          modelCode: '',
          status: 1,
          manufacturerCode: this.formData.manufacturerCode
        };
      },
      //编辑按钮触发弹框
      showEditDialog (row) {
        this.dialogAddOrUpdateModel = {
            show: true,
            title: "修改设备型号",
            option: "edit",
        };
        this.modelData = {
            modelId: row.modelId,
            modelName: row.modelName,
            modelCode: row.modelCode,
            status: row.status,
            manufacturerCode: this.formData.manufacturerCode
        };
      },
      //搜索
      searchFilter() {
        this.getDataList();
      },
    //新增提交
    handleSubmit() {
        addObj(this.modelData).then(data => {
            this.$notify.success('添加成功')
            this.visible = false
            this.dialogAddOrUpdateModel.show = false;
            this.getDataList()
        }).catch(() => {
            this.canSubmit = true;
        });
    },
    //修改提交
    handleSubmitEdit() {
        putObj(this.modelData).then(data => {
            this.$notify.success('修改成功')
            this.visible = false
            this.dialogAddOrUpdateModel.show = false;
            this.getDataList()
        }).catch(() => {
            this.canSubmit = true;
        });
    },
      // 删除
      deleteHandle (id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delObj(id)
        }).then(data => {
          this.$message.success('删除成功')
          this.getDataList()
        })
      },
  },
}
</script>

<style lang="scss" scoped>
</style>